/*
* $Id: QBeeInspectView.cpp 153 2010-09-26 17:33:07Z volok-aleksej@yandex.ru $
*
* Copyright 2009 by Volokitin Alexey
* All rights reserved
*
*/
#include "Common/include.h"
#include "QBeeInspectView.h"
#include "Components/Journal/QueenBeeManager.h"

#define CX_BUTTON		110
#define CY_BUTTON		30
#define SIZE_SCROLL		17
#define DIST_BUTTON		10
#define BUTTON_REGION	(CX_BUTTON + DIST_BUTTON)

#pragma warning(disable: 4407)

CQBeeInspectView::CQBeeInspectView(CState* pState)
: CQBeeView(pState){}
CQBeeInspectView::~CQBeeInspectView(){}

void CQBeeInspectView::SetState(){
	m_pState->m_nState = Journal::VIEW_INSPECT_QBEE;
}

bool CQBeeInspectView::IsViewState(State state){
	return state.m_nPosTab == Journal::QUEEN_BEE_JOURNAL &&
		state.m_type == Journal::NONE &&
		state.m_nTypeSel == Journal::FAMILY;
}

void CQBeeInspectView::Init(){
	m_infoDrawer.Clear();
	m_aNamesQB.clear();
	std::map<int, CQueenBeeManager::queen_bee> aQb = IJournalComponent::GetQueenBeeManager()->GetFamilyQB(m_pState->m_nIdParameter);
	for(std::map<int, CQueenBeeManager::queen_bee>::iterator it = aQb.begin(); it != aQb.end(); it++){
		int j = m_infoDrawer.AddItem(it->first);
		m_aNamesQB.insert(std::make_pair(it->first, it->second.s_strName));
		m_infoDrawer.AddSubItem(j, InfoItem(it->second.s_strName, _T("Arial"), 20, true, true));
		int liveid = it->second.s_bDead ? INFO_INSPECT_DEAD_QB_ID : INFO_INSPECT_LIVE_QB_ID;
		m_infoDrawer.AddSubItem(j, InfoItem(ILanguageManager::GetLanguageManager()->GetString(liveid), _T("Arial"), 18, true, false));
		m_infoDrawer.AddSubItem(j, InfoItem(it->second.s_strDescription, _T("Arial"), 16, false, true));
		std::tstring strTemp(ILanguageManager::GetLanguageManager()->GetString(INFO_INSPECT_FERT_QB_ID) + _T(" - "));
		m_infoDrawer.AddSubItem(j, InfoItem(strTemp + it->second.s_strFert, _T("Arial"), 14, false, false));
		strTemp = ILanguageManager::GetLanguageManager()->GetString(INFO_INSPECT_DATE_QB_ID) + _T(" - ");
		m_infoDrawer.AddSubItem(j, InfoItem(strTemp + CConvertFunction::ConvertFromDate(it->second.s_date), _T("Arial"), 14, false, false));
		strTemp = ILanguageManager::GetLanguageManager()->GetString(INFO_INSPECT_BREED_QB_ID) + _T(" - ");
		m_infoDrawer.AddSubItem(j, InfoItem(strTemp + 
			ILanguageManager::GetLanguageManager()->GetString(BREED_HYBRID_ID + it->second.s_nBreed), _T("Arial"), 14, false, false));
	}
}

BEGIN_MESSAGE_MAP(CQBeeInspectView, CQBeeView)
	ON_WM_CREATE()
	ON_WM_SIZE()
	ON_WM_PAINT()
	ON_WM_LBUTTONDOWN()
	ON_COMMAND(BUTTON_ADD_ID, OnAddCommand)
	ON_COMMAND(BUTTON_DELETE_ID, OnDeleteCommand)
	ON_COMMAND(BUTTON_CHANGE_ID, OnChangeCommand)
END_MESSAGE_MAP()

int CQBeeInspectView::OnCreate(LPCREATESTRUCT lpCreateStruct){
	Init();
	int bRes = __super::OnCreate(lpCreateStruct);
	m_buttonAdd.Create(ILanguageManager::GetLanguageManager()->GetString(ADD_ID).c_str(),
		WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON, CRect(), this, BUTTON_ADD_ID);
	m_buttonDelete.Create(ILanguageManager::GetLanguageManager()->GetString(DELETE_ID).c_str(),
		WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON, CRect(), this, BUTTON_DELETE_ID);
	m_buttonChange.Create(ILanguageManager::GetLanguageManager()->GetString(CHANGE_ID).c_str(),
		WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON, CRect(), this, BUTTON_CHANGE_ID);
	m_buttonChange.EnableWindow(FALSE);
	m_buttonDelete.EnableWindow(FALSE);
	return bRes;
}

void CQBeeInspectView::OnSize(UINT nType, int cx, int cy){
	CRect rect;
	GetClientRect(rect);
	m_buttonAdd.MoveWindow(CRect(rect.left + DIST_BUTTON, rect.bottom - CY_BUTTON - DIST_BUTTON,
		rect.left + BUTTON_REGION, rect.bottom - DIST_BUTTON), FALSE);
	m_buttonChange.MoveWindow(CRect(rect.left + BUTTON_REGION + DIST_BUTTON,
		rect.bottom - 40, rect.left + 2*BUTTON_REGION, rect.bottom - DIST_BUTTON), FALSE);
	m_buttonDelete.MoveWindow(CRect(rect.left + 2*BUTTON_REGION + DIST_BUTTON, rect.bottom - CY_BUTTON - DIST_BUTTON,
		rect.left + 3*BUTTON_REGION, rect.bottom - DIST_BUTTON), FALSE);
	__super::OnSize(nType, cx, cy - CY_BUTTON - 2 * DIST_BUTTON);
}

void CQBeeInspectView::OnLButtonDown(UINT nFlags, CPoint point){
	__super::OnLButtonDown(nFlags, point);
	if(m_infoDrawer.GetSel() != -1){
		m_buttonChange.EnableWindow(TRUE);
		m_buttonDelete.EnableWindow(TRUE);
	}else{
		m_buttonChange.EnableWindow(FALSE);
		m_buttonDelete.EnableWindow(FALSE);
	}
}

void CQBeeInspectView::OnPaint(){
	CPaintDC dc(this);
	CRect rect;
	GetClientRect(rect);
	PaintView(&dc, CRect(rect.left, rect.bottom - CY_BUTTON - 2 * DIST_BUTTON, rect.right, rect.bottom));
	rect.bottom -= CY_BUTTON + 2 * DIST_BUTTON;
	if(m_bVScroll)
		rect.right -= SIZE_SCROLL;
	if(m_bHScroll)
		rect.bottom -= SIZE_SCROLL;
	m_infoDrawer.Paint(&dc, rect);
	if(m_bHScroll && m_bVScroll){
		CBrush brush;
		brush.CreateSolidBrush(GetSysColor(COLOR_3DFACE));
		dc.FillRect(CRect(rect.right, rect.bottom,
			rect.right + SIZE_SCROLL, rect.bottom + SIZE_SCROLL), &brush);
		brush.DeleteObject();
	}
}

void CQBeeInspectView::OnAddCommand(){
	SetManipulation(Journal::ADD_QUEEN_BEE, m_pState->m_nIdParameter);
}

void CQBeeInspectView::OnDeleteCommand(){
	int nId = m_infoDrawer.GetId(m_infoDrawer.GetSel());
	std::tstring strContent = ILanguageManager::GetLanguageManager()->GetString(QUESTION_DELETE_ID) +
		_T(" '") + m_aNamesQB[nId] + _T("'.");
	if(MessageBox(strContent.c_str(), ILanguageManager::GetLanguageManager()->
		GetString(DELETE_QUEEN_BEE_ID).c_str(), MB_YESNO) == IDYES)
		SetManipulation(Journal::DELETE_QUEEN_BEE, nId);
}

void CQBeeInspectView::OnChangeCommand(){
	int nId = m_infoDrawer.GetId(m_infoDrawer.GetSel());
	SetManipulation(Journal::CHANGE_QUEEN_BEE, nId);
}


void CQBeeInspectView::SetManipulation(Journal::typeItemMnpl type, int nId){
	IWnd* pWnd = IVisualManager::GetVisualManager()->
		GetVisualComponent(ILanguageManager::GetLanguageManager()->GetString(JOURNAL_CAPTION_ID))->GetComponentFrame();
	pWnd->Post(UM_ITEM_MANIPULATION, type, nId);
}

void CQBeeInspectView::PaintView(CDC* dc, CRect rect){
	CPen pen;
	pen.CreatePen(PS_SOLID, 1, RGB(0, 0, 0));
	dc->MoveTo(rect.TopLeft());
	dc->LineTo(CPoint(rect.right - 1, rect.top));
	dc->LineTo(CPoint(rect.right - 1, rect.bottom - 1));
	dc->LineTo(CPoint(rect.left - 1, rect.bottom - 1));
	pen.DeleteObject();
	CBrush brush;
	brush.CreateSolidBrush(GetSysColor(COLOR_3DFACE));
	for(int i = 0; i < 3; i++){
		dc->FillRect(CRect(rect.left + i*BUTTON_REGION, rect.top + 1, rect.left + DIST_BUTTON + i*BUTTON_REGION, rect.bottom - 1), &brush);
		dc->FillRect(CRect(rect.left + i*BUTTON_REGION + DIST_BUTTON, rect.top + 1,
			rect.left + (i+1)*BUTTON_REGION, rect.top + DIST_BUTTON), &brush);
		dc->FillRect(CRect(rect.left + i*BUTTON_REGION + DIST_BUTTON, rect.bottom - DIST_BUTTON,
			rect.left + (i+1)*BUTTON_REGION, rect.bottom - 1), &brush);
	}
	dc->FillRect(CRect(rect.left + 3*BUTTON_REGION, rect.top + 1, rect.right - 1, rect.bottom - 1), &brush);
	brush.DeleteObject();
}
